﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Coomtools.dbhelper;
using Coomtools.commclass;
using Sunny.UI;
using System.IO;
using System.Windows.Forms;
using CYLScheck.UIcomm;

namespace CYLScheck
{
  

    public static class loadsetting

    {

      public static string ini_readcpuid = "readcpuid";
        public static string ini_readdiskid = "readdiskid";

        /// <summary>
        /// 第一次运行程序时自动创建loginset。ini 文件
        /// </summary>
        /// <returns></returns>
        public static void BeginNewSet()
        {
            ///用 sunnyui 的inifileex 工具类 写入  loginset.int 文件初始配置信息
            IniFileEx iniwrite = new IniFileEx("loginset.ini");
            iniwrite.Write("link", "servername", "127.0.0.1");
            iniwrite.Write("link", "Maindatabase", "zqsys");
            iniwrite.Write("link", "database", "zqdata_1");
            iniwrite.Write("link", "serverport", "1433");
            iniwrite.Write("link", "datauser", "sa");
            iniwrite.Write("link", "datapassword", "2219033");
            iniwrite.Write("link", "datauser", "sa");
           
            // 写入登录用户信息
            iniwrite.Write("user", "username", "Admin");
            //加密写入登录密码 默认为空
             MyEncrypt encrypt=new MyEncrypt();
           iniwrite.Write("user", "userpassword",encrypt.EncryptString("") );
            iniwrite.Write("user", "enableuser", true);

            #region  UIStyle 定义
            /*
             
                 public enum UIStyle
    {
        Custom = 0,
        Blue = 1,
        Green = 2,
        Orange = 3,
        Red = 4,
        Gray = 5,
        Purple = 6,
        LayuiGreen = 7,
        LayuiRed = 8,
        LayuiOrange = 9,
        DarkBlue = 101,
        Black = 102,
        Colorful = 999
    }
             
             */
            #endregion



            iniwrite.Write("user", "userUI",3 );


            //写入初始软件软件注册状态
            iniwrite.Write("regstats", "regcpuid",encrypt.EncryptString("") );
            iniwrite.Write("regstats", "regdskid", encrypt.EncryptString(""));
            iniwrite.Write("regstats", "regmacid", encrypt.EncryptString(""));
            iniwrite.Write("regstats", "regversionid", "1.2");
            iniwrite.Write("regstats", "reguserid", "共享用户");

        }

        /// <summary>
        /// 读取cpuid和硬盘id并加密 添加到全局检查变量中
        /// </summary>
        /// <returns>执行成功 并将 Gobleset.Gobel_IsRegUser 设置为 "YES"  双重检查 </returns>
        public static bool Check_IsReguser()
        {
            try
            {

            HardwareInfo GetHardwareinf = new HardwareInfo();
                MyEncrypt encrypt = new MyEncrypt();
            string regcpuid = "";
            regcpuid = GetHardwareinf.GetCpuID();
            Gobleset.Gobel_checkCpustr = regcpuid;
            string regdskid = "";
            regdskid =GetHardwareinf.GetFristHardDiskID();
            Gobleset.Gobel_checkDiskstr= regdskid;
                //macid 暂时不用与验证
                //string regmacid = "";
                //regmacid = encrypt.EncryptString(GetHardwareinf.GetPhysicLocalMacAddr());
                //Gobleset.Gobel_checkecyMacstr= regmacid;
                if (Gobleset.Gobel_checkCpustr=="" && Gobleset.Gobel_checkDiskstr=="")
                {
                    return false;
                }
                //cpuid 和硬盘号 有一个对 就认为 是注册用 用户
                if (Gobleset.Gobel_checkCpustr == ini_readcpuid || Gobleset.Gobel_checkDiskstr == ini_readdiskid)
                {
                    Gobleset.Gobel_IsRegUser = "YES";
                    return true;
                }
                else return false;
         


            }
            catch (Exception)
            {
                return false;
                throw;
            }
            

        }
        public static bool load_iniset()
        {
            try
            {
                //判断loginset.ini 是否存在不存异常处理 生成这个文件
                string[] readlogin = File.ReadAllLines(@"loginset.ini");
                //读取配置文件 把它读取给各个控制变量。
                //************************
                IniFileEx iniread = new IniFileEx("loginset.ini");
                //读取数据库连接字符串********************************************





                Gobleset.link_servername= iniread.Read("link", "servername", "66");
               Gobleset.link_serverport = iniread.Read("link", "serverport", "");
                Gobleset.link_databasename= iniread.Read("link", "database", "");
                Gobleset.link_datauser = iniread.Read("link", "datauser", "");
               Gobleset.link_datapassword= iniread.Read("link", "datapassword", "");
                 
                ///用于验证注册用户第二个数据库连接
               Gobleset.link_Maindatabase= iniread.Read("link", "Maindatabase", "");
               
                //*******************************************
                //&&&&&&&&&&&&&&&    以下其他配置参数&&&&&&&&&&&&&&&&&&&&&&

                //读取登录用户名和密码
                Gobleset.username = iniread.Read("user", "username", "Admin");
                //读取的是加密后的密码需要解密
                string userMiMapassword = iniread.Read("user", "userpassword", "");
                Gobleset.userpassword = new MyEncrypt().DecryptString(userMiMapassword);
                Gobleset.enableuser = iniread.ReadBool("user", "enableuser", true);
                //读取用户设置的UI编号
                Gobleset.userUI= iniread.ReadInt ("user", "userUI", 3);
                //读取ini文件里注册过的 disk号和cpu号并解密
                Gobleset.Gobel_reguserid = iniread.Read("regstats", "reguserid", "");
                MyEncrypt encrypt = new MyEncrypt();
            
                ini_readcpuid = encrypt.DecryptString(iniread.Read("regstats", "regcpuid", ""));
                ini_readdiskid = encrypt.DecryptString(iniread.Read("regstats", "regdskid", ""))   ;


                //*************************



                return true;
            }
            catch (Exception)
            {
                // MessageBox.Show("没有找到初始化文件 loginset.ini,退出重新打开会自动生成一个", "登录文件不存在", MessageBoxButtons.OK, MessageBoxIcon.Error);

                UIMessageBox.ShowWarning("没有找到初始化文件 loginset.ini,退出重新打开会自动生成一个. 根据具体情况修改！");

                BeginNewSet();
                return false;
                throw;
            }

        }
        
        /// <summary>
       /// 向loginset.ini文件中读取 配置
       /// </summary>
       /// <returns>true 为读取成功，false为读取失败并创建此初始文件</returns>
      
       public static bool load_databaseconstring()

        {
            if (load_iniset())
            {
            //链接参数自定义实现
            //连接验证主数据库 用于验证注册用户第二个数据库连接
            string sqlconnet2 = "Data Source =" + Gobleset.link_servername + "," + Gobleset.link_serverport + "; Initial Catalog=" + Gobleset.link_Maindatabase + "; User Id = " + Gobleset.link_datauser + "; Password=" + Gobleset.link_datapassword;
            sqlhelper.conString2 = sqlconnet2;

            //连接主工作数据库

            string sqlconnet = "Data Source =" + Gobleset.link_servername + "," + Gobleset.link_serverport + "; Initial Catalog=" + Gobleset.link_databasename + "; User Id = " + Gobleset.link_datauser + "; Password=" + Gobleset.link_datapassword;
            sqlhelper.connectionString = sqlconnet;
                return true;
            }
                return false;

         



        }


    }
}
